package com.ganlebao.study.springcloudfeignclient.basic.acl;


import feign.FeignException;
import feign.Request;
import feign.Response;
import feign.hystrix.FallbackFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;
import java.util.Optional;

@Component
public class GitHubQueryFallbackServiceFactory implements FallbackFactory<GitHubQueryService>, GitHubQueryService {

    private static final Logger logger = LoggerFactory.getLogger(GitHubQueryFallbackServiceFactory.class);

    @Override
    public GitHubQueryService create(Throwable cause) {
        logger.error("访问出现异常", cause);
        if (cause instanceof FeignException) {
            FeignException fex = (FeignException) cause;
            Request request = fex.request();
            String requestInfo = "未知";
            if (request != null) {
                requestInfo = request.toString();
            }
            logger.error("feign调用[{}]出错", requestInfo);
        }
        return new GitHubQueryFallbackServiceFactory();
    }

    @Override
    public String searchRepo(String keyValue) {
        return "访问出错";
    }

    @Override
    public ResponseEntity<byte[]> searchRepoUseGzip(String keyValue) {
        return ResponseEntity.noContent().build();
    }

    @Override
    public String notExistsPage(String keyValue) {
        return "访问的页面不存在";
    }
}
